package com.luxsoft.em2.converters;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.JsonSerializable;
import org.codehaus.jackson.map.JsonSerializableWithType;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.TypeSerializer;
import org.hibernate.HibernateException;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.luxsoft.siipap.model.core.Cliente;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.sw3.ventas.Pedido;

public class JSONConverter implements MessageConverter{
	
	private ObjectMapper mapper;
	public JSONConverter(){
		mapper=new ObjectMapper();
		//mapper.getSerializationConfig().addMixInAnnotations(Pedido.class, ClienteMixIn.class);
	}

	public Message toMessage(Object object, Session session)
			throws JMSException, MessageConversionException {
		try {
			String json=mapper.writeValueAsString(object);
			return session.createTextMessage(json);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}

	public Object fromMessage(Message message) throws JMSException,
			MessageConversionException {
		String json=((TextMessage)message).getText();
		try {
			return mapper.readValue(json, Object.class);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}
	

	//@JsonIgnoreProperties(value={"direcciones","comentarios","telefonos","contactos","cuentas","credito","autorizacionPagoContraEntrega","cobrador","vendedor"})
	abstract class ClienteMixIn {
		
		
		@JsonIgnore
		public abstract Cliente getCliente();
		
		
	}
	
	public static void main(String[] args) {
		
	}

}
